<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="record-form">

    <div class="layui-form-item">
        <label class="layui-form-label">实付金额</label>
        <div class="layui-input-block">
            <input type="text" name="payAmount" readonly value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">抵扣金额</label>
        <div class="layui-input-block">
            <input type="text" name="deductionAmount" readonly value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">分享佣金</label>
        <div class="layui-input-block">
            <input type="text" name="consumerShareReward" readonly value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">代理销售佣金</label>
        <div class="layui-input-block">
            <input type="text" name="foremanReward" readonly value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">实际退款金额</label>
        <div class="layui-input-block">
            <input type="text" name="refundAmount" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认退款</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    let id;// 订单记录id
    window.onload = function () {
        id = GetQueryString("id");
        if (isNotNull(id)) {
            init(id);
        } else {
            layer.msg('数据异常，请重试');
            setTimeout(function () {
                let iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
            }, 1500);
        }
    }

    /**
     * 初始化数据
     */
    function init(id) {
        $.ajax({
            type: "get",
            url: '/buying/purchase/commission/details/' + id,
            dataType: 'json',
            contentType: 'application/x-www-form-urlencoded',
            success: function (req) {
                let data = req.data;
                layui.use(['form'], function () {
                    let form = layui.form,
                        $ = layui.$;
                    let orderStatus = req.data.orderStatus;
                    if (orderStatus === '已退款') {
                        layer.msg('该订单已退款，不可重复操作');
                        setTimeout(function () {
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                        return;
                    }
                    // let refundAmount = parseFloat(req.data.payAmount) - parseFloat(req.data.consumerShareReward)
                    //     - parseFloat(req.data.foremanReward)
                    form.val('record-form', {
                        "payAmount": req.data.payAmount,
                        "consumerShareReward": req.data.consumerShareReward,
                        "foremanReward": req.data.foremanReward,
                        "refundAmount": req.data.payAmount,
                        "deductionAmount": req.data.deductionAmount,
                    });
                })
            },
            error: function (e) {
                layer.msg("数据有误");
            }
        })
    }

    layui.use(['layer', 'form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 监听提交
        form.on('submit(saveBtn)', function (data) {
            let refundAmount = parseFloat(data.field.refundAmount.trim())
            let payAmount = parseFloat(data.field.payAmount.trim())
            if (refundAmount > payAmount) {
                layer.msg("退款金额不能大于实际支付金额");
                return;
            }
            if (isNaN(refundAmount)) {
                layer.msg("退款金额数据格式错误");
                return;
            }
            var index = layer.alert("确定提交吗？", {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...", {
                    icon: 16,
                    time: -1
                })
                let requestData = {
                    'id': id,
                    'refundAmount': refundAmount.toFixed(2),
                }
                $.ajax({
                    type: "post",
                    url: `/buying/order/refund`,
                    data: JSON.stringify(requestData),
                    dataType: 'json',
                    contentType: 'application/json; charset=UTF-8',
                    success: function (req) {
                        layer.msg(req.msg);
                        layer.close(index);
                        // 关闭弹出层
                        setTimeout(function () {
                            layer.close(myMsg);//手动关闭
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                            // 调用父页面的方法
                            let parentWindow = window.parent;
                            parentWindow.reloadTable();
                        }, 1500);
                    },
                    error: function (e) {
                        layer.msg(e.msg);
                        layer.close(myMsg);//手动关闭
                    }
                })

            });

            return false;
        });

    });
</script>

</body>
</html>